
//页面背景动画
$(document).ready(function () {
    var stars = 1600;  /*星星的密集程度，数字越大越多*/
    var $stars = $(".stars");
    var r = 600;   /*星星的看起来的距离,值越大越远,可自行调制到自己满意的样子*/
    for (var i = 0; i < stars; i++) {
        var $star = $("<div/>").addClass("star");
        $stars.append($star);
    }
    $(".star").each(function () {
        var cur = $(this);
        var s = 0.2 + (Math.random() * 1);
        var curR = r + (Math.random() * 300);
        cur.css({
            transformOrigin: "0 0 " + curR + "px",
            transform: " translate3d(0,0,-" + curR + "px) rotateY(" + (Math.random() * 360) + "deg) rotateX(" + (Math.random() * -50) + "deg) scale(" + s + "," + s + ")"

        });
    });
});

//鼠标移动时停止动画
$(window).on('mousemove', function () {
    $(".stars").addClass("stop");
    setTimeout(function () {
        $(".stars").removeClass("stop");
    }, 2000);
});
//点击可切换密码可见性
$(".eye").click(function () {
    $(this).toggleClass("glyphicon-eye-close glyphicon-eye-open");
    if ($("#pwd_reg").attr("type") == "password") {
        $("#pwd_reg").attr("type", "text");
    } else {
        $("#pwd_reg").attr("type", "password");
    }
});
var uname_code = 0;
var pwd_code = 0;
var pwd_code1 = 0;
//注册时用户名为空、格式正确和不正确时的提示信息
$("#uname_reg").on('blur', function () {
    var reg = /^[a-zA-Z0-9_]{6,12}$/;
    if ($(this).val().trim() == '') {
        $(this).parent().siblings("p").html("用户名不能为空").removeClass("text-success").addClass("text-danger");
        uname_code = 0;
    } else if (reg.test($(this).val())) {
        $(this).parent().siblings("p").html("用户名格式正确").removeClass("text-danger").addClass("text-success");
        uname_code = 1;
    } else {
        $(this).parent().siblings("p").html("用户名格式错误，请修改").removeClass("text-success").addClass("text-danger");
        uname_code = 2;
    }
});
//注册时密码为空、格式正确和不正确时的提示信息
$("#pwd_reg").on('blur', function () {
    var reg = /^.*(?=.{6,})(?=.*\d)(?=.*[a-zA-Z])(?=.*[!@#$%^&*? ]).*$/;
    if ($(this).val().trim() == '') {
        $(this).parent().siblings("p").html("密码不能为空").removeClass("text-success").addClass("text-danger");
        pwd_code = 0;
    } else if (reg.test($(this).val())) {
        $(this).parent().siblings("p").html("密码格式正确").removeClass("text-danger").addClass("text-success");
        pwd_code = 1;
    } else {
        $(this).parent().siblings("p").html("密码格式错误，请修改").removeClass("text-success").addClass("text-danger");
        pwd_code = 2;
    }
});
//注册时确认密码与上面密码输入相符合时
$("#pwd_confirm").on('blur', function () {
    if ($(this).val() == $("#pwd_reg").val()) {
        $(this).parent().siblings("p").html("两次输入密码一致").removeClass("text-danger").addClass("text-success");
        pwd_code1 = 1;
    } else {
        $(this).parent().siblings("p").html("两次输入密码不一致，请修改为一致").removeClass("text-success").addClass("text-danger");
        pwd_code1 = 2;
    }
});

//在输入框中输入信息动态匹配包含关键字的微博
$(".search").on("input", function () {
    var con = $(this).val();
    var reg = new RegExp(con, 'g');
    $.ajax({
        url: "/query",
        method: "get",
        data: {
            dir: "2"
        },
        success: function (xhr) {
            if (xhr.length == 0) {
                swal('服务器出现未知的错误');
            } else if(con == ''){
                show(xhr);
            } else{
                var arr =[];
                xhr.forEach(function(ele){
                    var result = reg.exec(ele.content);
                    if(result){
                        arr.push(ele);
                    }
                });
                show(arr);

                $(".blogcon").css("color","red");

            }
        }
    });

});

//将符合条件的微博信息渲染到页面
function show(arr){
    $(".weibo").empty();
    arr.forEach(function(ele){
        var $con =
                        $(`<div class="container weibo_message" index="${ele._id.toString()}">
                            <div class="row col-sm-8 col-sm-offset-2">
                                <div class="panel panel-primary">
                                    <div class="panel-body">
                                        <h4>
                                            <img src="${ele.pic}" alt="">
                                            <i class="username">${ele.username}</i>
                                            <button type="button" class="close delete_ele" aria-hidden="true">&times;</button>
                                        </h4>
                                        <div class="blogcon">${ele.content}</div>
                                        <div class="comments">
                                            
                                        </div>
                                    </div>
                                    <div class="panel-footer clearfix">
                                        <div class="function">
                                            <span class="date text-success">${ele.date}</span>
                                            <i class="like text-danger glyphicon glyphicon-thumbs-up" index="0">
                                                <span class="num">${ele.like}</span>
                                            </i>
                                            <a href="#" class="text-warning remark" click="0">评论</a>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>`);
        $(".weibo").append($con);
    });
}